package com.zhengdongzulin.zuke.app.main.findhouse

import androidx.lifecycle.rxLifeScope
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.app.Api
import com.zhengdongzulin.zuke.app.data.City
import com.zhengdongzulin.zuke.app.data.FindHouse
import com.zhengdongzulin.zuke.base.BaseViewModel
import com.zhengdongzulin.zuke.parser.ObjectList
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class FindHouseVm : BaseViewModel() {

    var get_city_com_list: StateLiveData<MutableList<City>> = StateLiveData()

    fun get_city_com_list() {
        rxLifeScope.launch(
                {
                    var r = RxHttp.postJson(Api.GET_CITY_COM_LIST)
                            .toResponse<MutableList<City>>()
                            .await()
                    get_city_com_list.postValueAndSuccess(r)
                }, {
            assignment(it, get_city_com_list)
        }
        )
    }

    var position_list: StateLiveData<MutableList<ZKFindHouseActivity.Sel>> = StateLiveData()

    fun position_list(cityId: String?) {
        rxLifeScope.launch(
                {
                    var r = RxHttp.postJson(Api.POSITION_LIST)
                            .add("cityId", cityId)
                            .toResponse<MutableList<ZKFindHouseActivity.Sel>>()
                            .await()
                    position_list.postValueAndSuccess(r)
                }, {
            assignment(it, position_list)
        }
        )
    }

    var dic_list: StateLiveData<MutableList<ZKFindHouseActivity.Sel>> = StateLiveData()

    fun dic_list() {
        rxLifeScope.launch(
                {
                    var r = RxHttp.postJson(Api.DIC_LIST)
                            .add("mark", "5045ea9c-c341-446a-ab7b-a635e57f778d")
                            .toResponse<MutableList<ZKFindHouseActivity.Sel>>()
                            .await()
                    dic_list.postValueAndSuccess(r)
                }, {
            assignment(it, dic_list)
        }
        )
    }

    var looking: StateLiveData<ObjectList<FindHouse>> = StateLiveData()

    fun looking(cityId: String, data: Map<String, Any> = HashMap()) {
        rxLifeScope.launch(
                {
                    var r = RxHttp.postJson(Api.LOOKING)
                            .add("cityId", cityId)
                            .add("wechatId", mShareViewModel!!.apartmentTypeId.value!!)
                            .add("current", mCurrentPage)
                            .add("size", mPageSize)
                            .addAll(data)
                            .toResponse<ObjectList<FindHouse>>()
                            .await()
                    looking.postValueAndSuccess(r)
                }, {
            assignment(it, looking)
        }
        )
    }

}